/**
 * 
 */
package org.shortestpath.service.dao.util;

import org.shortestpath.core.Point;
import org.shortestpath.service.DbService;
import org.shortestpath.service.dao.DbDao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.postgis.Geometry;
import org.postgis.LineString;
import org.postgis.MultiLineString;
import org.postgis.PGgeometry;

/**
 * @author Thilani
 * 
 */
public class DbUtility2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String tableCreate = "CREATE TABLE simpleroads22 (gid serial NOT NULL, road_id bigint, x_cor double precision, "
                + "y_cor double precision, adj_nodes text, the_geom geometry,  CONSTRAINT " + "simplroadstable2_pkey PRIMARY KEY (gid))";

        DbDao dbDao = new DbDao();
        /*
         * try { dbDao.createSimpleTable(tableCreate); } catch (SQLException e1)
         * { // TODO Auto-generated catch block e1.printStackTrace(); }
         */

        ResultSet resultSet;
        try {
            resultSet = dbDao.getAllRoads();
            int gid = 0;
            int gidnew = 0;
            while (resultSet.next()) {
                /*
                 * Retrieve the geometry as an object then cast it to the
                 * geometry type. 
                 */
                PGgeometry pGgeometry = (PGgeometry) resultSet.getObject(1);
                int road_id = Integer.parseInt(resultSet.getObject(2).toString());

                if (pGgeometry.getGeoType() == Geometry.MULTILINESTRING) {
                    MultiLineString multiLineString = (MultiLineString) pGgeometry.getGeometry();
                    LineString[] lineStrings = multiLineString.getLines();
                    List<org.postgis.Point> multiLinePoints = new ArrayList<org.postgis.Point>();
                    int lineid = 0;

                    for (int j = 0; j < lineStrings.length; j++) {
                        LineString lineString = lineStrings[j];
                        org.postgis.Point[] roadPoints = lineString.getPoints();
                        for (int k = 0; k < roadPoints.length; k++) {
                            if(k == 0){
                                roadPoints[k].setSrid(0);
                            }else {
                                roadPoints[k].setSrid(gid);
                            }
                            multiLinePoints.add(roadPoints[k]);
                            gid++;
                            lineid++;
                        }

                    }

                    Iterator<org.postgis.Point> i = multiLinePoints.iterator();
                    int temp = 0;
                    while (i.hasNext()) {
                        temp = temp +1;
                        org.postgis.Point point = i.next();
                        String lastPart = "";
                        if (point.getSrid() == 0){
                            lastPart = String.valueOf(gidnew + 1);
                        }else{
                            lastPart = String.valueOf(point.getSrid() + 1);
                        }
                        
                        if (point.getSrid() + 1 >= multiLinePoints.size()) {
                            lastPart = String.valueOf(-1);
                        }
                        String adjTest = "'" + String.valueOf(point.getSrid() - 1) + "," + lastPart + "'";
                        String insertTable = "INSERT INTO simpleroads22(gid, road_id, x_cor, y_cor, adj_nodes) VALUES (" + gidnew + ", " + road_id + ", " + point.getX() + ", "
                                + point.getY() + ", " + adjTest + ");";
                        gidnew++;

                        dbDao.setSimpletableData(insertTable);
                    }

                } else {
                    // TODO Throw exception
                }
            }
            resultSet.close();
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        DbUtility2 db2 = new DbUtility2();
        db2.setAdjNodes();
    }

    public void setAdjNodes() {
        DbDao dbDao = new DbDao();
        try {
            ResultSet resultSet = dbDao.getAllPoints();
            while (resultSet.next()) {
                double x_cor = Double.parseDouble(resultSet.getObject(1).toString());
                double y_cor = Double.parseDouble(resultSet.getObject(2).toString());
                int g_id = Integer.parseInt(resultSet.getObject(3).toString());
                int road_id = Integer.parseInt(resultSet.getObject(4).toString());
                String adj_nodes = (String) resultSet.getObject(5).toString();

                Point point = new Point(x_cor, y_cor);
                point.setRoadId(road_id);
               // Collection<Point> intersectPoints = dbDao.getStIntersection(point, adj_nodes);
              
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    //TODO connects the roads by setting the adjacent vertexes

}
